<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'order:uf_mall_order:exportXls'" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出 </a-button>
        <!-- 高级查询 -->
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #bodyCell="{ column, record, index, text }"></template>
    </BasicTable>
    <!-- 表单区域 -->
    <UfMallOrderDetailDrawer @register="registerDetailDrawer" @success="handleSuccess" />
    <UfMallOrderRefundDrawer @register="registerRefundDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="order-ufMallOrder" setup>
  import { ref, reactive, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema, superQuerySchema } from './UfMallOrder.data';
  import {
    list,
    listOffline,
    deleteOne,
    batchDelete,
    getImportUrl,
    getExportUrl,
    getExportBatchDeliveryTemplateXls,
    getImportBatchDeliveryExcel,
  } from './UfMallOrder.api';
  import { useUserStore } from '/@/store/modules/user';
  import UfMallOrderDetailDrawer from '@/views/order/components/UfMallOrderDetailDrawer.vue';
  import UfMallOrderRefundDrawer from '@/views/order/components/UfMallOrderRefundDrawer.vue';
  import { useDrawer } from '@/components/Drawer';
  import { useMethods } from '@/hooks/system/useMethods';

  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  //注册model
  const [registerDetailDrawer, { openDrawer: openDetailDrawer }] = useDrawer();
  const [registerRefundDrawer, { openDrawer: openRefundDrawer }] = useDrawer();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '订单管理',
      api: listOffline,
      columns,
      canResize: false,
      showSummary: true,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [
          ['createTimeFieldTime', ['createTime_begin', 'createTime_end'], 'YYYY-MM-DD HH:mm:ss'],
          ['payTimeFieldTime', ['payTime_begin', 'payTime_end'], 'YYYY-MM-DD HH:mm:ss'],
        ],
      },
      actionColumn: {
        width: 200,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '线下订单',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  const { handleExportXls, handleImportXls } = useMethods();

  function handleExportDeliveryTemplate(params) {
    handleExportXls('批量发货模板', getExportBatchDeliveryTemplateXls, {});
  }

  function handleImportBatchDelivery(file) {
    handleImportXls(file, getImportBatchDeliveryExcel, reload);
  }

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openDetailDrawer(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openDetailDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openDetailDrawer(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }

  /**
   * 详情
   */
  function handleRefund(record: Recordable) {
    openRefundDrawer(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '发货',
        onClick: handleEdit.bind(null, record),
        auth: 'order:uf_mall_order:edit',
        ifShow: record.status == 2 && !record.refundSts,
      },
      {
        label: '提货',
        onClick: handleEdit.bind(null, record),
        auth: 'order:uf_mall_order:edit',
        ifShow: record.status == 2 && !record.refundSts,
      },
      {
        label: '处理退货/退款',
        onClick: handleRefund.bind(null, record),
        auth: 'order:uf_mall_order:edit',
        ifShow: record.refundSts == 1,
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
    ];
  }
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
